/*
 * @lc app=leetcode.cn id=144 lang=csharp
 *
 * [144] 二叉树的前序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
    public IList<int> PreorderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode current = root;
        IList<int> ans = new List<int>();
        while (current != null || stack.Count != 0)
        {
            if (current != null)
            {
                ans.Add(current.val);
                stack.Push(current);
                current = current.left;
            }
            else
            {
                current = stack.Pop().right;
            }
        }
        return ans;
    }
}
// @lc code=end

